package com.example.stumanager.student;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.stumanager.R;
import com.example.stumanager.database.MessageSql;

import java.util.ArrayList;
import java.util.Collections;
//留言
public class StudentMessagePage extends AppCompatActivity {
    EditText comment;
    Button submit,re;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_message_page);
        comment = findViewById(R.id.comment);
        submit = findViewById(R.id.submit);
        ListView list = findViewById(R.id.commentList);
        MessageSql db = new MessageSql(this);
        re = findViewById(R.id.re);
        re.setOnClickListener(view -> finish());
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (comment.getText().toString().equals("")) {
                    Toast.makeText(StudentMessagePage.this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                MessageSql db = new MessageSql(getBaseContext());
                if (db.add(comment.getText().toString())) {
                    Toast.makeText(StudentMessagePage.this, "添加成功", Toast.LENGTH_SHORT).show();
                    ArrayList array = db.getAll();
                    Collections.reverse(array);
                    ArrayAdapter<String> adapter1 = new ArrayAdapter<>(StudentMessagePage.this, android.R.layout.simple_list_item_1, array);
                    list.setAdapter(adapter1);
                } else {
                    Toast.makeText(StudentMessagePage.this, "添加失败", Toast.LENGTH_SHORT).show();
                }
            }
        });


        ArrayList array = db.getAll();
        Collections.reverse(array);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(StudentMessagePage.this, android.R.layout.simple_list_item_1, array);
        list.setAdapter(adapter1);


    }
}